﻿using LPBToolsLib.NetSocket;
using LPBToolsLib;
using StationSer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using StationNF;

namespace StationSer
{
    public class UDPPassiveBus : UDPServer,IDatasBus
    {
        public T getBus<T>()
        {
            object Temp = null;
            if (typeof(T) == typeof(UDPPassiveBus))
            {
                Temp = this;
            }
            return (T)Temp;
        }

        public BusTypes BusType { get { return BusTypes.UDPPassive; } }
        public Recorder_Error ErrRecorder { get; set; } = null;
        public string BusInfo
        {
            get
            {
                if (ListenEndPoint != null)
                    return ListenEndPoint.ToString() + ":UDP";
                else
                    return "None";
            }
        }
        public bool BusStatus()
        {
            return base.IsOnListen;
        }

        public UDPPassiveBus(EndPoint tListenEP) : base(tListenEP)
        {
            base.OnRecDatas += UDPBus_OnRecDatas;
        }

        public UDPPassiveBus(string BusInfo) : base(null)
        {
            base.OnRecDatas += UDPBus_OnRecDatas;
            IPAddress tBusIP = null;
            int iPort = 0;
            if (BusInfo.IndexOf(":") > 0)
            {
                string[] BusInfos = BusInfo.Split(':');
                if ((BusInfos.Length >= 2)
                    && IPAddress.TryParse(BusInfos[0], out tBusIP)
                    && int.TryParse(BusInfos[1], out iPort)
                    && (iPort > 0))
                    try
                    {
                        StartListen(new IPEndPoint(tBusIP, iPort));
                    }
                    catch (Exception ex)
                    {
                        ErrRecorder?.setRecord(ex);
                    }
            }
        }

        public UDPPassiveBus(Dictionary<string, object> BusInfo) : base(null)
        {
            base.OnRecDatas += UDPBus_OnRecDatas;
            IPAddress tBusIP = null;
            int iPort = 0;
            if (BusInfo != null)
            {
                if (BusInfo.ContainsKey("BusIP"))
                    IPAddress.TryParse(BusInfo["BusIP"].ToString(), out tBusIP);
                if ((tBusIP == null) && BusInfo.ContainsKey("Name"))
                    IPAddress.TryParse(BusInfo["Name"].ToString(), out tBusIP);
                if (BusInfo.ContainsKey("Port"))
                    int.TryParse(BusInfo["Port"].ToString(), out iPort);
                if ((iPort <= 0) && BusInfo.ContainsKey("BaudorPort"))
                    int.TryParse(BusInfo["BaudorPort"].ToString(), out iPort);
                if ((tBusIP != null) && (iPort > 0))
                    try
                    {
                        ListenEndPoint = new IPEndPoint(tBusIP, iPort);
                    }
                    catch (Exception ex)
                    {
                        ErrRecorder?.setRecord(ex);
                    }
            }
        }

        public void Start()
        {
            try
            {
                StartListen();
            }
            catch (Exception ex)
            {
                ErrRecorder?.setRecord(ex);
            }
        }

        /// <summary>
        /// 收到数据包事件，用于总线处理收到的数据包
        /// </summary>
        public event BusRecCallBack OnRecPacket;

        /// <summary>
        /// 屏蔽父级的数据接收事件，该事件在控件代码空间没有被调用
        /// </summary>
        public new event UDPRecDataCallBack OnRecDatas;

        private void UDPBus_OnRecDatas(object sender, EndPoint SourPoint, byte[] RecData)
        {
            byte[] SendDatas = null;
            if ((OnRecPacket != null) &&
                OnRecPacket(SourPoint,DExPacket.getSerExPacket(RecData), out SendDatas) &&
                (SendDatas != null))
            {
                SendRespon(SourPoint, SendDatas);
            }
        }

        public int putStation(PLCStation tNewStation)
        {
            return 0;
        }
    }

}
